package site.wetsion.galio.sdk.common;

import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 自定义 accessToken实体
 *
 * @author weixin
 * @version 1.0
 * @CLassName GalioOauthAccessToken
 * @date 2019/3/8 2:48 PM
 */
public class GalioOauthAccessToken implements Serializable {

    private static final long serialVersionUID = 1004874279895418692L;

    private String accessToken;

    /** 用于刷新的token **/
    private String refreshToken;

    private String tokenType = "bearer";

    private Date expiration;

    private String scope;

    private int validity = 0;

    private GalioOauthAccessToken next;

    private GalioOauthAccessToken pre;

    private UserDetails userDetails;

    private Map<String, Object> additionalInformation = Collections.emptyMap();

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public void setValue(String value) {
        this.accessToken = value;
    }

    /**
     * The token value.
     *
     * @return The token value.
     */
    public String getValue() {
        return accessToken;
    }

    public int getExpiresIn() {
//        return expiration != null ? Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L)
//                .intValue() : 0;
        return expiration != null ? this.validity : 0;
    }

    public void setExpiresIn(int delta) {
        this.validity = delta;
//        setExpiration(new Date(System.currentTimeMillis() + delta * 1000L));
    }

    public boolean isExpired() {
        return expiration != null && expiration.before(new Date());
    }

    /**
     * 是否接近要过期，即超过2/3时间，但还未过期
     *
     * @return boolean
     **/
    public boolean nearExpired() {
        return expiration != null &&
                System.currentTimeMillis() > (this.expiration.getTime() - this.validity * 1000L / 3) &&
                new Date().before(this.expiration);
    }

    public Date getExpiration() {
        return expiration;
    }

    public void setExpiration(Date expiration) {
        this.expiration = expiration;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public Map<String, Object> getAdditionalInformation() {
        return additionalInformation;
    }

    public void setAdditionalInformation(Map<String, Object> additionalInformation) {
        this.additionalInformation = new LinkedHashMap<String, Object>(additionalInformation);
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public GalioOauthAccessToken getNext() {
        return next;
    }

    public void setNext(GalioOauthAccessToken next) {
        this.next = next;
        this.expiration = next.getExpiration();
        this.validity = next.getExpiresIn();
        this.refreshToken = next.getRefreshToken();
    }

    public int getValidity() {
        return validity;
    }

    public void setValidity(int delta) {
        this.validity = delta;
    }

    public UserDetails getUserDetails() {
        return userDetails;
    }

    public GalioOauthAccessToken getPre() {
        return pre;
    }

    public void setUserDetails(UserDetails userDetails) {
        this.userDetails = userDetails;
    }

    public void setPre(GalioOauthAccessToken pre) {
        this.pre = pre;
    }
}
